package signGrammar.expressions;

import static signGrammar.expressions.util.CExpressionElement._;
import signGrammar.IShape;
import signGrammar.expressions.util.CExpressionElement;
import signGrammar.expressions.util.CExpressionElementArrayBuilder;
import signGrammar.expressions.util.UExpression;
import signGrammar.util.UShapeExpression;

public class CUnaryExpression implements IGrammarExpression{

	private String unarySign;
	private IGrammarExpression expression;
	public CUnaryExpression(String unarySign, IGrammarExpression expression) {
		this.unarySign = unarySign;
		this.expression = expression;
	}

	@Override public IShape evaluate(IGrammarContext context) {
		return UShapeExpression.eval(unarySign, expression.evaluate(context));
	}

	@Override public IGrammarExpression replace(String name, IGrammarExpression expr) {
		return UExpression.eval(unarySign, expression.replace(name, expr) );
	}

	@Override public CExpressionElement[] toElements() {
		return new CExpressionElementArrayBuilder()
			.add( _(unarySign) )
			.add(  expression.toElements() )
			.get();
	}

	@Override public IGrammarExpression clone(){
		return UExpression.eval(unarySign, expression);
	}
	
	@Override public String toString(){
		return unarySign + expression; 
	}
}
